#ifndef FFMPEGDECODE_H
#define FFMPEGDECODE_H

#include "mytcpsocket.h"
#include "RingBuffer.h"
#include <QObject>
#include <QImage>
#include <memory>

using std::unique_ptr;
using std::shared_ptr;

extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}

class FFmpegDecode : public QObject
{
    Q_OBJECT
public:
    explicit FFmpegDecode(const QString &msg, shared_ptr<RingBuffer> pSharedBuffer, QObject *parent = nullptr);
    ~FFmpegDecode();

signals:
    void initCodecFinished();
    void sigGetOneFrame(QImage img);

public slots:
    void startRtmp();
    void startPlayback();
    void process();
    void stop();

private slots:
    void handleCodecParams(Packet pkt);

private:
    void initVideoCodec(AVCodecParameters *pVideoCodecPar);
    void initAudioCodec(AVCodecParameters *pAudioCodecPar);

private:
    QString _msg; // 请求解码器参数时需要携带的V字段，实时流url或起始终止时间戳+摄像头id+通道号
    MyTcpSocket *_pTcpConnection;
    AVCodec *_pVideoCodec;
    AVCodecContext *_pVideoCodecCtx;
    AVCodec *_pAudioCodec;
    AVCodecContext *_pAudioCodecCtx;
    shared_ptr<RingBuffer> _pSharedBuffer;
    bool _isWorking;
};

#endif // FFMPEGDECODE_H
